package com.carsystems.controller;

import com.carsystems.mapper.CarDetailsMapper;
import com.carsystems.pojo.CarDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CarDetailsController {

    @Autowired
    CarDetailsMapper carDetailsMapper;

    //查看全部
    @GetMapping("/querCarList")
    public List<CarDetails> querCarList() {
        List<CarDetails> carDetails = carDetailsMapper.querCarList();
        return carDetails;
    }

    //按照car_id查询
    @GetMapping("/querCarByPrimaryKey")
    public CarDetails querCarByPrimaryKey(Integer car_id) {
        CarDetails carDetails = carDetailsMapper.querCarByPrimaryKey(car_id);
        return carDetails;
    }

    //增加一辆车
    @ResponseBody
    @PostMapping("/addCar")
    public String addCar(@RequestBody CarDetails car) {
        carDetailsMapper.addCar(car);
        if (car != null) {
            return "success";
        } else {
            return "fail";
        }
    }

    //修改车辆信息
    @ResponseBody
    @PostMapping("/updateCar")
    public String updateCar(@RequestBody CarDetails car){
        int row = carDetailsMapper.updateCar(car);
        carDetailsMapper.querCarByPrimaryKey(car.getCar_id());
        if (row==1){
            return "success";
        }else {
            return "fail";
        }
    }

    //删除公司
    @ResponseBody
    @GetMapping("/deleteCar")
    public String deleteCar(CarDetails car){
        int row = carDetailsMapper.deleteCar(car.getCar_id());
        if (row == 1){
            return "success";
        }else {
            return "fail";
        }
    }

}
